package com.example.genericrpg_android;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class GameView extends View implements OnTouchListener {
	private Context master;
	
	public static final int VIEW_WIDTH = 800;
	public static final int VIEW_HEIGHT = 600;
	
	public float touchX;
	public float touchY;
	public boolean move = false;
	
	GameEngine engine;
	
	public GameView(Context master) {
		super(master);		
		setFocusable(true);
        setFocusableInTouchMode(true); 
        this.setOnTouchListener(this);
        this.master = master; 
	}
	
	public void initialize() {        
        engine = new GameEngine(master, this);
        touchX = -1;
		touchY = -1;
	}
	
	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public GameView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		engine.render(canvas);
	}
	
	public void updateAll() {
		if (move)
			engine.handleDown(touchX, touchY);
		engine.update();
		invalidate();
	}
	
	public void pauseAll() {		
		engine.pause();
	}
	
	public void resumeAll() {
		if (engine != null)
			engine.resume();
	}
	
	public boolean onTouch(View view, MotionEvent event) {  
		
			switch(event.getAction()) {
	        case MotionEvent.ACTION_DOWN:
	        	touchX = event.getX();
	        	touchY = event.getY();
	        	engine.handleInput(touchX, touchY);
	        	break;
	        case MotionEvent.ACTION_UP:
	        	touchX = event.getX();
	        	touchY = event.getY();
	        	engine.handleUP(touchX, touchY);
	        	break;
	        case MotionEvent.ACTION_MOVE:
	        	break;
	        }
			
			return true;
    }
}

